package com.escom.service;

import com.androidplot.xy.XYSeries;
import com.escom.utils.Constantes;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.StringTokenizer;

/**
 * Created by Carlos on 18/04/2014.
 */
public class PlotElectroDynamic implements Runnable, XYSeries {

    private PlotObservable plotObservable;
    protected List<Double> valueElectro;
    private int sizeEje;

    public PlotElectroDynamic() {
        plotObservable = new PlotObservable();
        sizeEje = 30;
        initArray();
    }

    class PlotObservable extends Observable {
        @Override
        public void notifyObservers() {
            setChanged();
            super.notifyObservers();
        }
    }

   /* @Override
    public void run() {
        try {
            byte buf[] = new byte[32];
            MulticastSocket s = new MulticastSocket(Constantes.PORT_ELECTROCADIOGRAMA);
            s.joinGroup(InetAddress.getByName(Constantes.GROUP));
            DatagramPacket pack = new DatagramPacket(buf, buf.length);
            while (true) {
                s.receive(pack);
                byte[] data = pack.getData();
                String valueByte = new String(data);
                StringTokenizer stringTokenizer = new StringTokenizer(valueByte, ",");
                double value = Double.valueOf(stringTokenizer.nextToken());
                //Log.d(Constantes.LOG_ID, this.getClass().getName() + " value: " + value);
                valueElectro.add(value);
                valueElectro.remove(0);
                plotObservable.notifyObservers();
                //Thread.sleep(100); // decrease or remove to speed up the refresh rate
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/

    public void run() {
        try {
            byte buf[] = new byte[32];
            MulticastSocket s = new MulticastSocket(Constantes.PORT_ELECTROCADIOGRAMA);
            s.joinGroup(InetAddress.getByName(Constantes.GROUP));
            DatagramPacket pack = new DatagramPacket(buf, buf.length);
            while (true) {
                    s.receive(pack);
                    byte[] data = pack.getData();
                    String valueByte = new String(data);
                    StringTokenizer stringTokenizer = new StringTokenizer(valueByte, ",");
                    double value = Double.valueOf(stringTokenizer.nextToken());
                    //Log.d(Constantes.LOG_ID, this.getClass().getName() + " value: " + value);
                    valueElectro.add(value);
                    valueElectro.remove(0);
                    plotObservable.notifyObservers();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getTitle() {
        return "Electrocadiograma";
    }

    @Override
    public int size() {
        return 30;
    }

    @Override
    public Number getX(int index) {
        return index;
    }

    @Override
    public Number getY(int index) {
        return valueElectro.get(index);
    }

    public void initArray() {
        valueElectro = new ArrayList<Double>();
        for (int i = 0; i < sizeEje; i++) {
            valueElectro.add(0.0);
        }
    }

    public void addObserver(Observer observer) {
        plotObservable.addObserver(observer);
    }

}
